﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar.Controls;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace KP_CommonLibrary.KP_CommonControls
{
    public class KP_ShortDateInput : MaskedTextBoxAdv
    {
        string userInputPattern = "";
        public KP_ShortDateInput()
        {
            base.Size = new Size(80, 25);
            base.Font = KP_UIConfig.EditControlFont;
            base.BackColor = KP_UIConfig.EditBackColor;
            base.ForeColor = KP_UIConfig.EditForeColor;
            this.Mask = "00/00/0000";
            this.ValidatingType = typeof(DateTime);
            TextAlign = HorizontalAlignment.Right;
            if (!DesignMode && LicenseManager.UsageMode != LicenseUsageMode.Designtime)
            {

                this.KeyDown += new System.Windows.Forms.KeyEventHandler(KP_ShortDateInput_KeyDown);
                this.TypeValidationCompleted += new System.Windows.Forms.TypeValidationEventHandler(KP_ShortDateInput_TypeValidationCompleted);
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            this.TextMaskFormat = System.Windows.Forms.MaskFormat.IncludeLiterals;
            if ((int)keyData == '/')
            {

                int pos = SelectionStart;
                if (pos < 2)
                {
                    SelectionStart = 3;
                    return true;
                }
                if (pos > 2 && pos < 5)
                {
                    SelectionStart = 6;
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        protected override bool ProcessKeyMessage(ref Message m)
        {
            return base.ProcessKeyMessage(ref m);
        }
        protected override bool ProcessDialogChar(char charCode)
        {
            return base.ProcessDialogChar(charCode);
        }
        int WM_KEYFIRST = 0x100;
        int WM_KEYDOWN = 0x100;
        int WM_KEYUP = 0x101;
        int WM_CHAR = 0X102;
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == WM_CHAR && m.WParam.ToInt32() == '/')
            {
                int pos = SelectionStart;
                if (pos < 2)
                {
                    string temp = Text.Substring(0, 2);
                    string temp2 = Text.Substring(3);
                    temp = temp.Trim();
                    if (temp.Length == 0)
                        temp = DateTime.Now.ToString("dd");
                    else
                        temp = temp.PadLeft(2, '0');
                    Text = temp + "/" + temp2;

                    SelectionStart = 3;
                    return true;
                }
                if (pos > 2 && pos < 5)
                {
                    string temp = Text.Substring(0, 2);
                    string temp2 = Text.Substring(3, 2);

                    string temp3 = Text.Length > 7 ? Text.Substring(7) : "";

                    temp2 = temp2.Trim();
                    if (temp2.Length == 0)
                        temp2 = DateTime.Now.ToString("MM");
                    else
                        temp2 = temp2.PadLeft(2, '0');
                    Text = temp + "/" + temp2 + "/" + temp3;

                    SelectionStart = 6;
                    return true;
                }
            }
            return base.ProcessKeyPreview(ref m);
        }
        void KP_ShortDateInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {

        }

        public DateTime Value
        {
            get
            {
                try
                {
                    return DateTime.ParseExact(Text, "dd/MM/yyyy", null);
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
        }
        void KP_ShortDateInput_TypeValidationCompleted(object sender, System.Windows.Forms.TypeValidationEventArgs e)
        {
            try
            {
                this.TextMaskFormat = System.Windows.Forms.MaskFormat.ExcludePromptAndLiterals;
                if (!String.IsNullOrEmpty(Text))
                {
                    this.TextMaskFormat = System.Windows.Forms.MaskFormat.IncludeLiterals;
                    String[] arr = Text.Split('/');
                    string partern = "dd/MM";
                    if (arr[0].Trim().Length == 0)
                    {
                        arr[0] = DateTime.Now.ToString("dd");
                    }
                    arr[0] = arr[0].Trim().PadLeft(2, '0');

                    if (arr[1].Trim().Length == 0)
                    {
                        arr[1] = DateTime.Now.ToString("MM");
                    }
                    arr[1] = arr[1].Trim().PadLeft(2, '0');
                    arr[2] = arr[2].Trim();

                    if (arr[2].Length == 0)
                    {
                        arr[2] = DateTime.Now.ToString("yyyy");
                        partern += "/yyyy";
                    }
                    else
                        partern += "/y".PadRight(2 + arr[2].Length - 1, 'y');


                    Text = String.Join("/", arr);
                    userInputPattern = partern;
                    Text = DateTime.ParseExact(Text, partern, null).ToString("dd/MM/yyyy");
                }
            }
            catch (Exception ex)
            {
                KPMessageBox.Show("Giá trị ngày không đúng", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);
                this.Focus();
                this.SelectionStart = 0;
                this.SelectionLength = this.TextLength;
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // KP_ShortDateInput
            // 
            // 
            // 
            // 
            this.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            this.Mask = "00/00/0000";
            this.Text = "  /  /";
            this.ValidatingType = typeof(System.DateTime);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}
